#!/usr/bin/env ruby -wKU
#encoding: UTF-8

module Model
  
  class TNovel
    
    attr_accessor :author
    attr_accessor :tiebaName
    attr_accessor :novelName
    attr_accessor :onTop # if crawl chapter only on top
    attr_accessor :maxVolNo # max volume number of this novel
    #attr_accessor :maxChpNo # is chpNo here necessary?
    attr_accessor :volumes # reference to list of volumes
    
    def initialize
      @maxVolNo = 0
      #@chpNo = 0
      @author = ""
      @tiebaName = ""
      @novelName = ""
      @volumes = []
	    @onTop = true
    end
    
    def addVolume(volume)
      @volumes << volume unless @volumes.include? volume
    end
    
    def getVolumeByVolNo(volNo)
      vol = @volumes.find do |vol|
        vol.volNo == volNo
      end
      vol = @volumes.last if vol.nil?
    end
    
    def getVolume(volNo, volName)
      volNameTmp = volName.nil? ? "" : volName
      @volumes.find do |vol|
        vol.volNo == volNo && vol.volName == volNameTmp
      end
    end
    
    def storageDir
      "storage/" + @author + "/" + @novelName
    end
    
    def ==(o)
      o.class == self.class && o.state == state 
    end
    alias_method :eql?, :==
    
    def hash
      state.hash
    end
    
    def state
      [@author, @tiebaName, @novelName]
    end
    
  end
  
end
